home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9920 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: s02.pavilion.co.uk!usenet
  2. From: AJRobb@pavilion.co.uk (Andy J Robb)
  3. Newsgroups: comp.lang.asm.x86,comp.lang.c,comp.lang.c++
  4. Subject: Re: Catch Fixed Point Overflow in C or C++
  5. Date: Tue, 05 Mar 1996 04:05:25 GMT
  6. Organization: Pavilion Internet plc
  7. Message-ID: <4hgegp$2qq@s02.pavilion.co.uk>
  8. References: <DnJpxH.CDw.0.net@indra.com>
  9. NNTP-Posting-Host: poolc35.pavilion.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. sullivan@indra.com (Steve Sullivan) wrote:
  13.  
  14. >Is it possible to have a Pentium assembler routine set up so
  15. >that fixed-point overflow in C or C++ is caught?  For example,
  16. >would either of the following approaches work?
  17.  
  18. >Approach 1:
  19. >   /* here carryflag() is an asm routine to return the carry flag */
  20. >   int sum, i, j;
  21. >   long lprod, li, lj;
  22. >   sum = i + j;
  23. >   if ( carryflag()) printf("sum overflow occured\n");
  24. >   lprod = li * lj;
  25. >   if ( carryflag()) printf("prod overflow occured\n");
  26.  
  27. -----BEGIN PGP SIGNED MESSAGE-----
  28.  
  29. The carry flag is not used to detect overflow in signed arithmetic -
  30. e.g:
  31.  
  32. (-1) + (-1)
  33.  
  34. will set the carry flag but the result can be represented correctly as
  35. -2 in type int.  The carry flag is used in multi-precision arithmetic
  36. to allow handling of large numbers (outside the range of a single
  37. register).
  38.  
  39. For signed arithmetic you should look at the overflow flag.
  40.  
  41. It may be possible to stay within C or C++ by trying the reverse
  42. operation:
  43.  
  44. sum = i + j;
  45. if (sum - j != i) printf("sum overflow occured\n");
  46. lprod = li * lj;
  47. if (lj && lprod/lj != li) printf("prod overflow occured\n");
  48.  
  49. Regards,
  50. Andy Robb.
  51.  
  52. -----BEGIN PGP SIGNATURE-----
  53. Version: 2.6.2i
  54.  
  55. iQCVAwUBMTu9N5Pl4P16x9sNAQHnOgQAuDR4sMdUrmIFL5Mx0IZNzcAabFztwUme
  56. yonyqFreOYRxJf4ym/ife3bmWzOJXmgkz84oorjXogqmRAafvLGSg80CiHPClIDd
  57. Rfl0UXeRMJThNa1V1FaKdOnhR6hQfpBdVgqk7vfcS62dtzh/TpUH+BaoabYF3mfq
  58. fUOY0YTU2sk=
  59. =I8xm
  60. -----END PGP SIGNATURE-----
  61.  
  62. -----BEGIN PGP PUBLIC KEY BLOCK-----
  63. Version: 2.6.2i
  64.  
  65. mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
  66. MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
  67. B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
  68. tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
  69. IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
  70. =/wVD
  71. -----END PGP PUBLIC KEY BLOCK-----
  72.  
  73.